//
// Created by dddgzc on 2025/1/24.
//

#include "../include/MyTimer.h"

#include <cstdio>

void MyTimer::resetTime() {
    this->passTime = 0;
    this->isShotted = false;
}

void MyTimer::setWaitTime(int waitTime) {
    this->waitTime = waitTime;
}

void MyTimer::setIsPaused(bool paused) {
    this->isPaused = paused;
}

void MyTimer::setIsOneShot(bool oneShot) {
    this->isOneShot = oneShot;
}

void MyTimer::setCallback(std::function<void()> callback) {
    this->myTimerCallback = callback;
}

void MyTimer::onUpdate(int delta) {
    if (isPaused) { // 暂停中
        return;
    }
    passTime += delta;
    // printf("passTime = %d\n", passTime);
    if (passTime >= waitTime) {
        // 不是单次触发 或者 是单次触发并且已经还没触发
        if ((!isOneShot || (isOneShot && !isShotted)) && myTimerCallback) {
            myTimerCallback();
        }
        isShotted = true;
        passTime = 0;
    }
}

void MyTimer::resume() {
    this->isPaused = false;
}

void MyTimer::pause() {
    this->isPaused = true;
}


